package com.music.demo.controller;

import com.music.demo.model.Music;
import com.music.demo.model.Product;
import com.music.demo.repository.MusicMapper;
import io.woo.htmltopdf.HtmlToPdf;
import io.woo.htmltopdf.HtmlToPdfObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author chenpeimin
 * @date 2019-05-11 12:01
 */
@Controller
public class MusicController {

    @Autowired
    private MusicMapper mapper;

    @Autowired
    private HtmlToPdf htmlToPdf;

    @GetMapping("/")
    public String index(@RequestParam(defaultValue = "all") String type,
                        @RequestParam(defaultValue = "all") String singer,
                        Model model) {
        System.out.println("type = " + type);
        System.out.println("singer = " + singer);
        List<String> typeList = mapper.selectType();
        List<String> singerList = mapper.selectSinger();

        List<Music> list = mapper.selectMusic(type, singer);

        model.addAttribute("typeList", typeList);
        model.addAttribute("singerList", singerList);
        model.addAttribute("musicList", list);
        model.addAttribute("s_type", type);
        model.addAttribute("s_singer", singer);

        return "index";
    }

    @GetMapping("/listMusic")
    @ResponseBody
    public List<Music> list(@RequestParam(defaultValue = "all") String type,
                            @RequestParam(defaultValue = "all") String singer) {
        List<Music> list = mapper.selectMusic(type, singer);
        return list;
    }

    @PostMapping("/deleteMusic")
    @ResponseBody
    public Boolean delete(@RequestParam Integer musicId) {
        if (musicId == null) {return Boolean.FALSE;}

        int count = mapper.deleteMusic(musicId);
        if (count > 0) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

    @PostMapping("/sell")
    @ResponseBody
    public Integer sell(@RequestParam String qrcode, @RequestParam Integer count) {
        if (qrcode == null) {return -1;}
        System.out.println("code = " + qrcode);
        int num = mapper.sellProd(qrcode, count);
        if (num == 0) {
            return -1;
        } else {
        	return mapper.getStock(qrcode);
        }
    }

    @GetMapping("/stock")
	@ResponseBody
	public Integer stock(@RequestParam String qrcode) {
    	Integer count = mapper.getStock(qrcode);
    	if (count == null) {
    		return 0;
		} else {
    		return count;
		}
	}

    @GetMapping("/product")
    public String gettProduct(Model model) {
        List<Product> list = mapper.selectProduct();
        model.addAttribute("products", list);
        return "product";
    }

    @PostMapping("/pdf")
    @ResponseBody
    public String pdf(@RequestParam String url) {
        System.out.println("url = " + url);
        String aa = "http://localhost:8080/?type=all&singer=周杰伦";
        htmlToPdf.object(HtmlToPdfObject.forUrl(aa)).convert("test.pdf");
        return "ok";
    }

    @GetMapping("/passpprt")
    public String login() {
        return "login";
    }

    @PostMapping("/passpprt")
    public String loginOk(HttpServletRequest request) {
        String account = request.getParameter("AccountID");
        System.out.println("AccountID = " + account);
        return "loginOk";
    }
}
